package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.entity.Purchase;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.mapper.PurchaseMapper;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yang
 * @description 针对表【buy_list(采购单)】的数据库操作Service实现
 * @createDate 2024-03-27 15:04:07
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {


    @Autowired
    private PurchaseMapper purchaseMapper;

    //添加采购单
    @Override
    public Result savePurchase(Purchase purchase) {
        //补充字段：给fact_buy_Num字段实际采购数量赋值为Buy_num字段的值预采购数量
        //purchase.setFactBuyNum(purchase.getBuyNum());

        //添加采购单
        int i = purchaseMapper.insertPurchase(purchase);
        if (i > 0) {
            return Result.ok("采购单添加成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单添加失败");
    }

    //分页查询采购单
    @Override
    public Page queryPurchasePage(Page page, Purchase purchase) {
        //查询采购单行数
        Integer purchaseCount = purchaseMapper.findPurchaseCount(purchase);

        //分页查询采购单
        List<Purchase> purchasePage = purchaseMapper.findPurchasePage(page, purchase);

        //组装分页信息
        page.setTotalNum(purchaseCount);
        page.setResultList(purchasePage);

        return page;
    }

    //根据id删除采购单
    @Override
    public Result deletePurchaseById(Integer integer) {
        int i = purchaseMapper.removePurchaseById(integer);
        if (i >0){
            return Result.ok("删除采购单成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除采购单失败!");
    }

    //根据id修改预计采购数量和实际采购数量
    @Override
    public Result updatePurchaseById(Purchase purchase) {
        int i = purchaseMapper.setNumById(purchase);
        if(i>0){
            return Result.ok("采购单修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改采购单失败");
    }
}




